/**
******************************************************************************************
* @file Sender.c
* @brief Send Message one by one by TCP/IP. It is used by ProxyTab.
* @date  4 avril 2015
* @author Benjamin Lancinot
*****************************************************************************************/
/* COPYRIGHT ****************************************************************************
*****************************************************************************************
* Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
*    list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************************/

#include "Sender.h"

/*******************************************************************************************
* PROTOTYPES
********************************************************************************************/

void Sender_setSocket(Sender *, int);

void Sender_destroy(Sender **);

int Sender_sendUI(Sender *, unsigned char, float);

/*******************************************************************************************
* FUNCTIONS
********************************************************************************************/

/**
 * @brief Constructor
 * @return Pointer to a new Sender
 **/
Sender* Sender_create(){
	Sender* This = (Sender*) malloc(sizeof(*This));

	This->clientSocket = -1;
	This->initialized = true;
	This->setSocket = Sender_setSocket;
	This->sendMessage = Sender_sendUI;
	This->destroy = Sender_destroy;

	return This;
}


/**
 * @brief Setter for the client socket
 * @param This Pointer to the wanted Sender
 * @param socket The new client socket
 **/
void Sender_setSocket(Sender *This, int socket){
	This->clientSocket = socket;
}


/**
 * @brief Destructor
 * @param This Double pointer to the wanted Sender
 **/
void Sender_destroy(Sender **This){

	if (*This) {
		free(*This);
		*This = NULL;
	}

}


/**
 * @brief Send a message by the connection tcp/ip
 * @param This Pointer to the wanted Sender
 * @param id The type of message
 * @param value The message to send
 * @return developement of the writing in the socket
 **/
int Sender_sendUI(Sender *This, unsigned char id, float value){

	int ret;

	// Frame for carry around the message
	char trame[TRAME_SIZE];
	memset(trame,0,sizeof(trame));

	// Add value and id into the frame
	sprintf(trame,"%d/%0.1f\n", id, value);

	//Send the frame
	if(This->initialized == true){
		ret = write(This->clientSocket, trame,strlen(trame));
	}
	return ret;
}

